<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;
use EasyWeChat\Kernel\Messages\Text;

/**
 * Class User
 *
 * @package App
 * @mixin \Eloquent
 */
class User extends Model
{

    protected $table = 't_member';
    public $timestamps = false;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    const BANSTATUS_ON = 1;
    const BANSTATUS_OFF = 2;

    static $BanStatusDisplay = [
        self::BANSTATUS_ON => '正常',
        self::BANSTATUS_OFF => '禁言',
    ];

    const SIGN_YES = 1;
    const SIGN_NO = 2;

    static $SignStatusDisplay = [
        self::SIGN_YES => '真实用户',
        self::SIGN_NO => '虚拟用户',
    ];


    static function PostingsUsers()
    {
        return DB::table('t_member')->select('c_id', 'name', 'uuid')->orderByDesc('c_id')->where('sign', self::SIGN_NO)->get()->toArray();
    }


    /**
     * 禁言
     * @param $c_id
     * @param $request
     * @return array
     * @throws \Exception
     */
    static function Ban($c_id, $request)
    {
        DB::beginTransaction();

        if (!User::where('c_id', $c_id)->update([
            'ban_status' => User::BANSTATUS_OFF,
            'ban_reason' => $request->ban_reason,
            'ban_time' => time() + ($request->ban_time * 24 * 3600)
        ])) {
            DB::rollBack();

            return ['status' => 2, 'message' => '禁言失败'];
        }
        //添加系统消息
        $message = Curl::PushOneMessage($c_id, $request->ban_reason);
        if ($message['status'] !== 1) {
            DB::rollBack();

            return $message;
        }
//        if (!DB::table('jpush')->insert(
//            [
//                'title' => '系统消息',
//                'content' => $request->ban_reason,
//                'ctime' => date('Y-m-d H:i:s', time()),
//                'user_id' => $c_id
//            ]
//        )) {
//            DB::rollBack();
//
//            return ['status' => 2, 'message' => '发送系统消息失败'];
//        }
//
//        if (!Curl::PushNum($c_id)) {
//            DB::rollBack();
//
//            return ['status' => 2, 'message' => '发送系统消息失败'];
//        }
        //推送
        //如果关联了公众号
/*        if ($open_id = User::where('c_id', $c_id)->value('wx_open_id')) {
            $message = new Text('aaa');
            $app = Wechat::OfficialConfig();
            $result = $app->customer_service->message($message)->to($open_id)->send();
//            if ($result['errcode'] !== 0 || $result['errmsg'] !== 'ok') {
//                DB::rollBack();
//
//                return ['status' => 2, 'message' => $result['errmsg']];
//            }
        }
        */

//        $a = Curl::GetCurl(Curl::CURL_URI . Curl::CURL_ORDERPUSH, ['alias' => $c_id, 'content' => $request->ban_reason, 'pcs' => 3]);
        DB::commit();

        return $message;
    }
}
